home *** CD-ROM | disk | FTP | other *** search
/ Asymetrix Multimedia Toolbook 4.0 (CBT Edition) / Asymetric Multimedia Toolbook 4.0 (CBT Edition).iso / ctb40mt.z / DBASE.ATS < prev    next >
Text File  |  1995-11-13  |  11KB  |  400 lines

  1. ACTION    "Link TB40DB3.DLL"
  2. BEHAVIOR    "Links to the functions in TB40DB3.DLL."
  3. CATEGORIES    dBaseIII
  4. {
  5.     sysSuspend = FALSE
  6.     clear sysError
  7.     linkdll sysToolBookDirectory & "tb40DB3.DLL"
  8.         INT checkDBIndex(STRING)
  9.         INT closeAllDBFiles()
  10.         INT closeDBFile(STRING)
  11.         INT closeDBIndexFile(STRING)
  12.         WORD createDBFieldTag(WORD)
  13.         INT createDBFile(STRING,WORD,WORD)
  14.             INT createDBIndexFile(STRING,STRING,WORD,WORD)
  15.         INT deleteDBFile(STRING)
  16.         INT deselectDBIndexFile()
  17.         INT findDBKey(STRING)
  18.         INT firstDBKey()
  19.         INT firstDBRecord()
  20.         INT freeDBFieldTag(WORD)
  21.         STRING getDBDateFormat()
  22.         STRING getDBErrorString(INT)
  23.         INT getDBFieldCount()
  24.         STRING getDBFieldName(WORD)
  25.         INT getDBFieldPrecision(STRING)
  26.         INT getDBFieldType(STRING)
  27.         STRING getDBFieldValue(STRING)
  28.         INT getDBFieldWidth(STRING)
  29.         STRING getDBFileName()
  30.         STRING getDBIndexExpression()
  31.         STRING getDBIndexFileName()
  32.         INT getDBKeyType()
  33.         STRING getDBKeyValue()
  34.         INT getDBNavigateToDeleted()
  35.         LONG getDBRecordCount()
  36.         INT getDBRecordDeleted()
  37.         LONG getDBRecordNumber()
  38.         INT gotoDBRecord(DWORD)
  39.         INT lastDBKey()
  40.         INT lastDBRecord()
  41.         INT nextDBKey()
  42.         INT nextDBRecord()
  43.         INT openDBFile(STRING)
  44.         INT openDBIndexFile(STRING)
  45.         INT packDBFile()
  46.         INT previousDBKey()
  47.         INT previousDBRecord()
  48.         INT reindexDBFile(STRING)
  49.         INT removeDBRecords(DWORD,DWORD)
  50.         INT selectDBFile(STRING)
  51.         INT selectDBIndexFile(STRING)
  52.         INT setDBDateFormat(STRING)
  53.         INT setDBFieldTag(WORD,WORD,STRING,STRING,WORD,WORD)
  54.         INT setDBFieldValue(STRING, STRING)
  55.         INT setDBNavigateToDeleted(INT)
  56.         INT setDBRecordDeleted(WORD)
  57.         INT writeDBRecord(DWORD)
  58.     end linkdll
  59.      if sysError <> null
  60.         request "Could not link to TB40db3.dll"
  61.     end    if
  62. }
  63.  
  64. SCRIPT "Create a dbf file."
  65. BEHAVIOR "An example of the code needed to create a dBaseIII file."
  66. CATEGORIES    dBaseIII
  67. {
  68. to handle createDB3
  69. -- this is an example, most dbf files must be custom tailored.
  70.     set fileName to "mydata.dbf"
  71.  
  72. -- We create a template that each record will use.
  73. -- Names, number, widths, and types of the fields all 
  74. -- must be carefully synchronized
  75.     set fNames to "firstName,lastName,employeeID," & \
  76.       "birthDate,insurance,comments"
  77.     set fNumber to itemCount(fNames)
  78.  
  79. -- "C" is character, "N" is numeric, "D" is date, 
  80. -- "L" is logical, and "M" is memo.
  81.     set fTypes to "C,C,N,D,L,M"
  82.  
  83. -- Date, logical and memo do not require widths.
  84.     set fWidths to "14,14,10,0,0,0"
  85.  
  86. -- Now we allocate a structure and get a "tag number" for it.
  87.     set tagNumber to createDBFieldTag(fNumber) 
  88.     if tagNumber <> 0
  89.  
  90. -- Now we use the tag for the structure and the data for the fields
  91. -- to tell the DLL what the records will look like.
  92.         step i from 1 to fNumber
  93.             get setDBFieldTag(tagNumber, i, item i of fNames, \
  94.               item i of fTypes, item i of fWidths, 0)
  95.             if it <= 0
  96.                 request getDBErrorString(it)
  97.                 break 
  98.             end if
  99.         end step
  100.     else
  101.         request "CreateDBFieldTag failure"
  102.         break
  103.     end if
  104.  
  105. -- Now we actually create the .dbf file that knows about this kind of record.
  106. -- The last parameter allows ToolBook to write over an
  107. -- existing file. "1" would prevent this.
  108.     get createDBFile(fileName,tagNumber,0)
  109.     if it <= 0
  110.         request getDBErrorString(it)
  111.         break
  112.     end if
  113.     get freeDBFieldTag(tagNumber)
  114.     get closeDBFile(fileName)
  115. end createDB3
  116. }
  117.  
  118. SCRIPT "Create a dBase index"
  119. BEHAVIOR "An example for creating an index for a .dbf file."
  120. CATEGORIES    dBaseIII
  121. {
  122. to handle createDB3Index
  123.     set fileName to "myData.ndx"
  124.  
  125. -- The art of designing a good index is in setting the  
  126. -- key expression correctly.
  127.     set myExpression to "lastName+firstName"
  128.  
  129. -- The index is created using the currently selected dBase file.
  130.     get createDBIndexFile(fileName,myExpression,0,0)
  131.     if it < 1
  132.         request getDBErrorString(it)
  133.     end    if
  134. end createDB3Index
  135. }
  136.  
  137. SCRIPT "Search for a string in DBIII"
  138. BEHAVIOR "An example for using an index to search a .dbf file."
  139. CATEGORIES    dBaseIII
  140. {
  141. -- Simple wrapper for findDBKey. Provides some feedback to the user for two
  142. -- special cases. FindDBKey returns 3 if it can't find the text, and returns 4
  143. -- if the search text goes past the last key.
  144. to handle findKey searchString
  145.     get findDBKey(searchString)
  146.     conditions
  147.     when it = 3
  148.         request "String not found, next key becomes the current key."
  149.     when it = 4
  150.         request "String greater than last key, last key becomes the current key."
  151.         -- belt and suspenders
  152.         get lastDBKey()
  153.     when it < 1
  154.         request getDBErrorString(it)
  155.     end    conditions
  156. end    findKey
  157. }
  158.  
  159. SCRIPT "Get the current key"
  160. BEHAVIOR "An example for getting the value of the current key of an index for a .dbf file."
  161. CATEGORIES    dBaseIII
  162. {
  163. -- Simple wrapper for getDBKeyValue
  164. to get keyValue
  165.     return getDBKeyValue()
  166. end    keyValue
  167. }
  168.  
  169. SCRIPT "Get the total record count"
  170. BEHAVIOR "An example for finding the total number of records in a .dbf file"
  171. CATEGORIES    dBaseIII
  172. {
  173. -- Simple wrapper for getDBRecordCount
  174. to get totalRecords
  175.     return getDBRecordCount()
  176. end totalRecords
  177. }
  178.  
  179. SCRIPT "Select a dBIII index"
  180. BEHAVIOR "An example for selecting between open indexes of a .dbf file."
  181. CATEGORIES    dBaseIII
  182. {
  183. -- Wrapper for indexing, handles errors until it gives up.
  184. to set index to value
  185.     -- make sure when you pass the parameter "value"
  186.     -- you include the path
  187.  
  188.     get checkDBIndex(value)
  189.     if it = 1
  190.         get selectDBIndexFile(value)
  191.     else
  192.         get reindexDBFile(value)
  193.         if it < 1
  194.             get getDBErrorString(it)
  195.         end    if
  196.     end    if
  197. end    index
  198. }
  199.  
  200. SCRIPT "Navigate in a dBIII Database"
  201. BEHAVIOR "An example for navigation in a dBIII database."
  202. CATEGORIES    dBaseIII
  203. {
  204. -- Simple wrapper for dBase navigation
  205. -- Possible values to pass are:
  206. -- first, last, next, previous, or a valid record number.
  207. -- For example: "set currentRecord to "last""
  208. to set currentRecord to value
  209.     set it to -1
  210.  
  211.     conditions 
  212.     when value = "first"
  213.         get firstDBKey()
  214.     when value = "last"
  215.         get lastDBKey()
  216.     when value = "next"
  217.         get nextDBKey()
  218.     when value = "previous"
  219.         get previousDBKey()
  220.     else
  221.     -- check to see that "value" is valid
  222.         if isType(Real, value) and value > 0
  223.             get goToDBRecord(value)
  224.         end if
  225.     end conditions
  226.  
  227.     if it < 0
  228.         if getDBRecordDeleted(currentRecord()) = 1    -- calls the "to get currentRecord" function
  229.               request "Record marked as deleted. Shall I" & CRLF & TAB &\
  230.              "Unmark the record for you," & CRLF & TAB &\
  231.              "Leave it marked, but let you navigate to it," & CRLF & TAB &\
  232.              "Or pack the database and delete the record."  \
  233.               with "Unmark" or "Leave Marked" or "Pack and Delete"
  234.             conditions
  235.             when it is "Unmark"
  236.                 get setDBRecordDeleted(0) -- undelete
  237.                 get goToDBRecord(value)
  238.             when it is "Leave Marked"
  239.             -- remember old value
  240.                 set NavToDel to getDBNavigateToDeleted()
  241.             -- get set and go
  242.                 get setDBNavigateToDeleted(1)
  243.                 get goToDBRecord(value)
  244.             -- reset old value
  245.                 get setDBNavigateToDeleted(NavToDel)
  246.             when it is "Pack and Delete"
  247.                 sysCursor = 4
  248.                 get packDBFile()
  249.                 if it < 1
  250.                     request getDBErrorString(it)
  251.                 else
  252.             -- some default behavior, nothing but something to do
  253.                     get firstDBKey()
  254.                 end if
  255.             end conditions
  256.         else
  257.             request getDBErrorString(it)
  258.         end if
  259.     end if
  260. end currentRecord
  261.  
  262. -- We need a corresponding "to get" handler, but this is very simple
  263. to get currentRecord
  264.     get getDBRecordNumber()
  265.     if it < 1
  266.         request getDBErrorString(it)
  267.     end if
  268.  
  269.     -- calling handler would need to check this value to see if 
  270.     -- an error has occured.
  271.     return it
  272. end currentRecord
  273. }
  274.  
  275. SCRIPT "Set the value of a field in dBIII"
  276. BEHAVIOR "An example for setting the value of a field in a .dbf file"
  277. CATEGORIES    dBaseIII
  278. {
  279. -- Simple wrapper for setDBFieldValue,
  280. -- A call to it might look like:
  281. -- "set fieldValue(firstName) to "Jimmy"", or
  282. -- "fieldValue(lastName) = "Carter"". 
  283. -- It sets the field in the current    record.
  284. to set fieldValue fieldName to value
  285.     get setDBFieldValue(fieldName, value)
  286.     if it < 0
  287.         request getDBErrorString(it)
  288.     end if
  289. end fieldValue
  290. }
  291.  
  292. SCRIPT "Get the value of a DBIII field"
  293. BEHAVIOR "An example for getting the value of a field in a .dbf file"
  294. CATEGORIES    dBaseIII
  295. {
  296. -- Simple wrapper for getDBFieldValue
  297. -- typically fieldName would be passed in a to get handler like:
  298. -- "get fieldValue("lastName")", which would return a last name.
  299. to get fieldValue fieldName
  300.     clear sysError
  301.     get getDBFieldValue(fieldName)
  302.     if sysError is not null
  303.         request getDBErrorString(syserror)
  304.     else
  305.         return it
  306.     end if
  307.     return null
  308. end fieldValue
  309. }
  310.  
  311. SCRIPT "Create a new DBIII record"
  312. BEHAVIOR "An example for creating a new record in a database"
  313. CATEGORIES    dBaseIII
  314. {
  315. -- Creates a new (and empty) record.
  316. to handle newDBRecord
  317. -- We create a new record by writing past the end of the database.
  318.     get writeDBRecord(getDBRecordCount() + 1)
  319.     if it < 0
  320.         request getDBErrorString(it)
  321.     end if
  322. end newDBRecord
  323. }
  324.  
  325. SCRIPT "Write to a DBIII database"
  326. BEHAVIOR "An example for writing a record out to a .dbf file."
  327. CATEGORIES    dBaseIII
  328. {
  329. -- Simple wrapper for writeDBRecord. This is where the dBase record 
  330. -- is actually written out. The handler expects you to pass the 
  331. -- record number of the record that is being written, so this would
  332. -- be called like this: "send writeDB getDBRecordNumber()", or with any
  333. -- other value that would evaluate to a record number.
  334. to handle writeDB rNumber
  335.     get writeDBRecord(rNumber)
  336.     if it < 0
  337.         request getDBErrorString(it)
  338.     end if
  339. end writeDB
  340. }
  341.  
  342. SCRIPT "Delete a dBIII record"
  343. BEHAVIOR "An example for marking and deleting a record from a .dbf file."
  344. CATEGORIES    dBaseIII
  345. {
  346. -- Simple wrapper for setDBRecordDeleted. Note also we pack the dBase here, 
  347. -- there's no recovery for a deleted file.
  348. to handle deleteRecord
  349. -- This marks the current record as "deleted"
  350.     get setDBRecordDeleted(1)
  351.     if it < 0
  352.         request getDBErrorString(it)
  353.     end    if
  354.  
  355. -- You may not want the next line if you want to be able to 
  356. -- recover from this operation.    It is possible to undelete a marked record,
  357. -- but not after you have packed the database.
  358. -- Note that this refers to a ToolBook handler, not a DLL function.
  359.     send packDatabase
  360. end deleteRecord
  361. }
  362.  
  363. SCRIPT "Pack a dBIII File"
  364. BEHAVIOR "An example for packing a dBase file and any associated, open indexes."
  365. CATEGORIES    dBaseIII
  366. {
  367. -- Simple wrapper for packDBFile
  368. to handle packDatabase
  369.     sysCursor = 4
  370. -- Operates on the current dBase
  371.     get packDBFile()
  372.     if it < 1
  373.         request getDBErrorString(it)
  374.     end if
  375.     sysCursor = default
  376. end packDatabase
  377. }
  378.  
  379. SCRIPT "Turn a ToolBook field into a dBase field"
  380. BEHAVIOR "An appropriately named field can quickly become a window on a DBIII database by placing these handlers in the script of a field, and broadcasting the appropriate messages."
  381. CATEGORIES    dBaseIII
  382. {
  383. -- Get and set data for and from these fields
  384. notifyBefore updateDisplay
  385.     -- this call the "to get fieldValue" function
  386.     -- found under the name of "Get the value of a DBIII field" in tb40.ats
  387.     my text = fieldValue(my name) of this book
  388. end updateDisplay
  389.  
  390. notifyBefore updateDB
  391.     -- this call the "to set fieldValue" handler
  392.     -- found under the name of "Set the value of a field in DBIII " in tb40.ats
  393.     fieldValue(my name) of this book = my text
  394. end updateDB
  395.  
  396. notifyBefore clearFields
  397.     clear my text
  398.     fieldValue(my name) of this book = null
  399. end clearFields
  400. }